capture log close
clear all
set maxvar 120000
set more off
pause off
# delimit;

/* Convert outreg estimates to dta file, add means (exact age) */

/* ******************** */
/* **** PREP MEANS **** */
/* ******************** */

local demo_list 	 "all female male record nonment singno othmen intel phys  nonphys twopar parhigh parlow white black nonw firstlt6 first7to13 firstgt13 " ;
local demo_abbr_list "a   f      m    c  		s      o      i     p     y		  t      h       l	  	w     b     z	   f1	   f2		 f3 " ;
local demo_list2 	 	"ue97high 	ue97low uegrhigh 	uegrlow ueallhigh 	uealllow crimehigh 	crimelow 	demhigh demlow 	pophigh	poplow 	" ;
local demo_abbr_list2 	"u7h 		u7l 	ugh 		ugl 	uah 		ual 	 ch 		cl 			dh 		dl 		ph 		pl 		" ;


local a_label "all" ;
local o_label "othmen" ;
local n_label "nonment" ;
local i_label "intel" ;
local p_label "phys" ;
local y_label "nonphys" ;
local t_label "twopar" ;
local s_label "singno" ;
local m_label "male" ;
local f_label "female" ;
local w_label "white" ;
local b_label "black" ;
local z_label "nonw" ;
local h_label "parhigh" ;
local l_label "parlow" ;
local f1_label "firstlt6" ;
local f2_label "first7to13" ;
local f3_label "firstgt13" ;
local c_label "record" ;
local u7h_label "ue97high" ;
local u7l_label "ue97low" ;
local ugh_label "uegrhigh" ;
local ugl_label "uegrlow" ;
local uah_label "ueallhigh" ;
local ual_label "uealllow" ;

foreach file in summstats summstats2 { ;

	use prwora_crime_`file'_exactage, clear ;

	keep if label == "all_mean"  	| label == "all_sd" 	| label == "all_median" | 
			label == "prec_mean" 	| label == "postt_mean"	| label == "prec_rd_mean" ;

	xpose, clear varname ;

	rename v1 mean_all ;
	rename v2 sd_all ;
	rename v3 median_all ;
	rename v4 mean_trt ;
	rename v5 mean_cntrl ;
	rename v6 mean_rd ;

	drop if _varname == "label" ;

	rename _varname variables ;

	order variables mean_all sd_all median_all mean_trt mean_cntrl mean_rd ;

	gen demo = substr(variables, 1, 1) ;

	replace variables = substr(variables,2,.);

	foreach demo in `demo_abbr_list' `demo_abbr_list2' { ;
		
		preserve ;
		
		keep if demo == "`demo'" ;

		drop demo ;

		tempfile means_prworaexact_``demo'_label' ;

		save `means_prworaexact_``demo'_label'' ;
		
		restore ;
	} ;
} ;





/* ************************************************ */
/* **** CONVERT IV ESTIMATES TO STATA DTA FILE **** */
/* ************************************************ */

foreach cov in no all{;

	foreach n of local num {;

		foreach exp in prworaexact {;
	
			if "`n'" == "1" | "`n'" == "2" {;
	
				foreach demo in `demo_list' `demo_list2' {;
				
					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};

					gen sample = "age18unfav" if n_age18unfav != . ;
					capture replace sample = "onssi1922" if n_onssi1922 != . ;
					pause ;
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;


					save `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo', replace ;
					

				};
			
			};

			else if "`n'" == "3" | "`n'" == "4" {;
	
				foreach demo in all {;
				
					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};

					gen sample = "age18unfav" if n_age18unfav != . ;
					capture replace sample = "onssi1922" if n_onssi1922 != . ;

					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					
					save `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo', replace ;
				};
			
			}; /* end if */
			
		}; /* end exp */
	
	}; /* end n*/

}; /* end cov*/




/* ************************************************ */
/* **** CONVERT RD ESTIMATES TO STATA DTA FILE **** */
/* ************************************************ */

foreach cov in no all{;

	foreach exp in prworaexact {;
		
		foreach n of local num {;
			
			if "`n'" == "1" | "`n'" == "2" {;
			
				foreach demo in `demo_list' `demo_list2' {;

					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					save `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo', replace ;
				
				};
			
			};
			
			if "`n'" == "3" | "`n'" == "4" {;
			
				foreach demo in all {;

					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					#delim;
					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					save `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo', replace ;
				
				};
			};
		};
	};
};

